#include "customTabStyle.h"


QSize CustomTabStyle::sizeFromContents(ContentsType type, const QStyleOption *option,
                       const QSize &size, const QWidget *widget) const
{
    QSize s = QProxyStyle::sizeFromContents(type, option, size, widget);
    if (type == QStyle::CT_TabBarTab) {
        s.transpose();
        s.rwidth() = 200; // 设置每个tabBar中item的大小
        s.rheight() = 70;
    }
    return s;
}


void CustomTabStyle::drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
    if(element==CE_TabBarTabLabel){
        if(const QStyleOptionTab *tab=qstyleoption_cast<const QStyleOptionTab *>(option)){
            QRect allRect=tab->rect;
            allRect.setWidth(allRect.width() - 5);
            allRect.setHeight(allRect.height() - 5);

            painter->setRenderHint(QPainter::Antialiasing, true);
            painter->setRenderHint(QPainter::SmoothPixmapTransform, true);

            if(tab->state&QStyle::State_Selected){
                painter->save();
                painter->setPen(0x4caf50);

                painter->setBrush(QBrush(0x4caf50));
                QPainterPath path;
                path.addRoundedRect(allRect, 10, 10);
                painter->drawPath(path);
                painter->restore();
            }
            else if(tab->state & QStyle::State_MouseOver){
                painter->save();
                painter->setPen(0x4caf50);
                painter->setBrush(QBrush(0x4caf50));
                painter->drawRoundedRect(allRect, 10, 10);
                painter->restore();
            }
            else
            {
                painter->setBrush(QBrush(0x000001));
            }
            QTextOption option;
            option.setAlignment(Qt::AlignCenter);
            if(tab->state&QStyle::State_Selected){
                painter->setPen(0xf8fcff);
            }else{
                painter->setPen(0x000001);
            }
            painter->drawText(allRect,tab->text,option);
            return ;
        }
    }
    if(element==CE_TabBarTab){
        QProxyStyle::drawControl(element,option,painter,widget);
    }
}
